/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rumbleToolkit;

import java.util.Hashtable;
import org.blinkenlights.jid3.ID3Exception;
import org.blinkenlights.jid3.ID3Tag;
import org.blinkenlights.jid3.MediaFile;
import org.blinkenlights.jid3.v1.ID3V1_0Tag;
import org.blinkenlights.jid3.v2.ID3V2_3_0Tag;

/**
 * Classe qui contiendra tout les methodes spécialistes dans l'analyse du mp3
 * @author Nordinator
 */
public class Mp3AnalyzerTool {
    
    /**
     * Methode qui analyse un mp3 et donne toute les caractéristiques sur 
     * sur celui ci
     * @return 
     */
    public static Hashtable<String, String> mp3analyzer(MediaFile oMediaFile) {
        Hashtable<String, String> res = new Hashtable();
        try {
            ID3Tag[] aoID3Tag = oMediaFile.getTags();
            // let's loop through and see what we've got
            // (NOTE:  we could also use getID3V1Tag() or getID3V2Tag() methods, if we specifically want one or the other)
            for (int k = 0; k < aoID3Tag.length; k++) {
                // check to see if we read a v1.0 tag, or a v2.3.0 tag (just for example..)
                if (aoID3Tag[k] instanceof ID3V1_0Tag) {
                    ID3V1_0Tag oID3V1_0Tag = (ID3V1_0Tag) aoID3Tag[k];
                    // does this tag happen to contain a title
                    if (oID3V1_0Tag.getTitle() != null) {
                        res.put("title", oID3V1_0Tag.getTitle());
                    }
                    if (oID3V1_0Tag.getArtist() != null) {
                        res.put("artiste", oID3V1_0Tag.getArtist());
                    }
                    if (oID3V1_0Tag.getAlbum() != null) {
                        res.put("album", oID3V1_0Tag.getAlbum());
                    }
                    if (oID3V1_0Tag.getAlbum() != null) {
                        res.put("genre", oID3V1_0Tag.getGenre().toString());
                    }
                    //TODO : ajouter
                    return res;
                } else if (aoID3Tag[k] instanceof ID3V2_3_0Tag) {
                    ID3V2_3_0Tag oID3V2_3_0Tag = (ID3V2_3_0Tag) aoID3Tag[k];
                    // check if this v2.3.0 frame contains a title, using the actual frame name
                    if (oID3V2_3_0Tag.getTIT2TextInformationFrame() != null) {
                        res.put("title", oID3V2_3_0Tag.getTIT2TextInformationFrame().getTitle());
                    }
                    // but check using the convenience method if it has a year set (either way works)
                    try {
                        res.put("artiste", oID3V2_3_0Tag.getArtist());
                        res.put("album", oID3V2_3_0Tag.getAlbum());
                    } catch (Exception e) {
                        System.out.println("Could get read year from tag: " + e.toString());
                    }
                }
            }
        } catch (ID3Exception e) {
            System.out.println("Format du MP3 non lu\n");
        } catch (Exception e) {
            System.out.println("Erreur externe à la librairie \n");
        }
        return res;
    }
}
